﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Xml;
using System.Web.Configuration;

namespace MpSoft.Basic.Configuration
{
    public class MpConfig : IConfigurationSectionHandler
    {
        #region Fields
        private static string _ConnectionString = "";
        private static string _EncryptionPrivateKey = "";
        private static bool _Initialized = false;
        private static int _CookieExpires = 128;
        #endregion

        #region Methods
        /// <summary>
        /// Creates a configuration section handler.
        /// </summary>
        /// <param name="parent">Parent object.</param>
        /// <param name="configContext">Configuration context object.</param>
        /// <param name="section">Section XML node.</param>
        /// <returns>The created section handler object.</returns>
        public object Create(object parent, object configContext, XmlNode section)
        {
            XmlNode node1 = section.SelectSingleNode("SqlServer");
            if (node1 != null)
            {
                XmlAttribute attribute = node1.Attributes["ConnectionStringName"];
                if ((attribute != null) && (WebConfigurationManager.ConnectionStrings[attribute.Value] != null))
                {
                    _ConnectionString = WebConfigurationManager.ConnectionStrings[attribute.Value].ConnectionString;
                }
            }
            XmlNode node2 = section.SelectSingleNode("Encryption");
            if (node2 != null)
            {
                XmlAttribute attribute1 = node2.Attributes["PrivateKey"];
                if (attribute1 != null)
                {
                    _EncryptionPrivateKey = attribute1.Value.ToString();
                }
                if (_EncryptionPrivateKey.Length == 0)
                {
                    _EncryptionPrivateKey = "ASDdk34Jahdsl2D%";
                }
            }

            return null;
        }

        /// <summary>
        /// Initializes the NopConfig object
        /// </summary>
        public static void Init()
        {
            if (!_Initialized)
            {
                ConfigurationManager.GetSection("MpConfig");
                _Initialized = true;
            }
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the connection string that is used to connect to the storage
        /// </summary>
        public static string ConnectionString
        {
            get
            {
                return _ConnectionString;
            }
            set
            {
                _ConnectionString = value;
            }
        }

        /// <summary>
        /// Gets or sets the encryption private key
        /// </summary>
        public static string EncryptionPrivateKey
        {
            get
            {
                return _EncryptionPrivateKey;
            }
            set
            {
                _EncryptionPrivateKey = value;
            }
        }

        /// <summary>
        /// Gets or sets the expiration date and time for the Cookie in hours
        /// </summary>
        public static int CookieExpires
        {
            get
            {
                return _CookieExpires;
            }
            set
            {
                _CookieExpires = value;
            }
        }
        #endregion
    }
}
